(function(KKBus){

    function ScheduleController() {
        var self = this;
        
        self.schedule = ko.observableArray();
        
        self.getSchedule = function ()
        {
             self.schedule.removeAll();
            console.log("duaaaa");
            app.request++;
        
             var url = ' rest/workplans';
                               
           
        
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.schedule.push(new KKBus.ScheduleModel(jsData[i].userId,jsData[i].day, jsData[i].startHour, jsData[i].endHour, jsData[i].id )); 
         }
         app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + ScheduleController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
        
       
      
       
           self. deleteSchedule = function(id, index) {
               
               self.schedule.remove(self.schedule()[index]);
               
                KKBus.Ajax.doDelete('/rest/workplan/'+id, function(result, textStatus, jqXHR) {
             });   
             };
    
         self.validation = ko.observable(false);
         self.errorMessage = ko.observable("Blad");
         self.success = ko.observable(false);   
            
             
         self.addSchedule = function() {
                   
          var url = 'rest/workplan/';
                 
            var schedule = JSON.stringify({
                day:$('#formSDay').val(),
                userId: $('#formSUserId').val(),
                startHour: $('#formSStartHour').val(),
                endHour: $('#formSEndHour').val()
             });
             
             
             if(KKBus.Validation.length($('#formSDay').val())
                && KKBus.Validation.length($('#formSUserId').val())
                && KKBus.Validation.length($('#formSStartHour').val())
                && KKBus.Validation.length($('#formSEndHour').val())){
             
                if(KKBus.Validation.godz($('#formSStartHour').val())
                    && KKBus.Validation.godz($('#formSEndHour').val())){
                    
                  
                    
                                         KKBus.Ajax.doPost(url, schedule, function(result, textStatus, jqXHR) {
                                             if (result == "ADDED") {
                                                 
                                                 $('#formSDay').val(' ');
                                                 $('#formSUserId').val(' ');
                                                 $('#formSStartHour').val(' ');
                                                 $('#formSEndHour').val(' ');
                                                 self.success(true);
                                                 setTimeout(function() 
                                                {   
                                        
                                                    self.success(false); 
                                                                 
                                                }, 3000);
                                                
                                                   var temp = JSON.parse(schedule);
                                                                 
                                          var ScheduleAdd = new KKBus.ScheduleModel(temp.userId,temp.day,temp.startHour,temp.endHour);

                                          self.schedule.push(ScheduleAdd);     
                                                 
                                                 
                                                }
                                                });
                       
                                                
                    }else{
                        
                        self.validation(true);
                        self.errorMessage("Błednie podany format godziny!");
                        setTimeout(function() { self.validation(false); }, 3000);
                        
                    };
                                      
                }else{
                  
                    self.validation(true);
                    self.errorMessage("Puste pola!");
                    setTimeout(function() { self.validation(false); }, 3000);                     
                                         
                };
                                      
             };  
             
             
          }
   

    KKBus.ScheduleController =  ScheduleController;
    
}(window.KKBus));

